package com.ios.controller;

import com.ios.dao.DisablerLocationMapper;
import com.ios.dto.DisablerLocationExecution;
import com.ios.dto.QueryLocationExecution;
import com.ios.dto.RelativeLocationExecution;
import com.ios.entity.QueryLocation;
import com.ios.enums.LocationStateEnum;
import com.ios.service.impl.DisablerLocationServiceImpl;
import com.ios.service.impl.QueryLocationServiceImpl;
import com.ios.service.impl.RelativeLocationServiceImpl;
import com.ios.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.Oneway;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/gps" ,method = {RequestMethod.POST})
public class GpsController {
    @Autowired
    private DisablerLocationServiceImpl disablerLocationService;
    @Autowired
    private RelativeLocationServiceImpl relativeLocationService;
    @Autowired
    private QueryLocationServiceImpl queryLocationService;

    @RequestMapping(value = "/adddisablergps")
    @ResponseBody
    public Map<String,Object> disablerGPS(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        Integer disabler_id = HttpServletRequestUtil.getInt(request,"disabler_id");
        String latitude = HttpServletRequestUtil.getString(request,"latitude");
        String longitude = HttpServletRequestUtil.getString(request,"longitude");
        if (disabler_id!=-1 && !latitude.equals(null) && !longitude.equals(null)){
            DisablerLocationExecution dl = disablerLocationService.AddDisablerLocation(disabler_id,latitude,longitude);
            if (dl.getState()== LocationStateEnum.SUCCESS_ADD.getState()){
                modelMap.put("success",true);
                modelMap.put("disablerLocationInfo",dl.getDisablerLocation());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",dl.getStateInfo());
            }
        }else {
            modelMap.put("success",false);
            modelMap.put("errMsg","请输入完整参数");
        }
        return modelMap;
    }

    @RequestMapping(value = "/addrelativegps")
    @ResponseBody
    public Map<String,Object> relatvieGPS(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        Integer relative_id = HttpServletRequestUtil.getInt(request,"relative_id");
        String latitude = HttpServletRequestUtil.getString(request,"latitude");
        String longitude = HttpServletRequestUtil.getString(request,"longitude");
        if (relative_id!=-1 && !latitude.equals(null) && !longitude.equals(null)){
            RelativeLocationExecution rl = relativeLocationService.AddRelativeLocation(relative_id,latitude,longitude);
            if (rl.getState()== LocationStateEnum.SUCCESS_ADD.getState()){
                modelMap.put("success",true);
                modelMap.put("relativeLocationInfo",rl.getRelativeLocation());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",rl.getStateInfo());
            }
        }else {
            modelMap.put("success",false);
            modelMap.put("errMsg","请输入完整参数");
        }
        return modelMap;
    }

    @RequestMapping(value = "/querydisabler")
    @ResponseBody
    public Map<String,Object> queryDisabler(HttpServletRequest request) throws ParseException {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        Integer disablerId = HttpServletRequestUtil.getInt(request,"disabler_id");
        Integer relativeId = HttpServletRequestUtil.getInt(request,"relative_id");
        if (disablerId!=-1 && relativeId!=-1){
            DisablerLocationExecution dl = disablerLocationService.QueryDisablerLocation(disablerId);
            QueryLocationExecution ql = queryLocationService.AddQueryLocation(disablerId,relativeId,0);
            if (dl.getState()==LocationStateEnum.SUCCESS_QUERY.getState()&&ql.getState()==LocationStateEnum.SUCCESS_ADDQUERY.getState()){
                modelMap.put("success",true);
                modelMap.put("queryRecord",ql.getQueryLocation());
                modelMap.put("disablerLocation",dl.getDisablerLocation());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",dl.getStateInfo());
            }
        }else {
            modelMap.put("success",false);
            modelMap.put("errMsg","请输入完整信息");
        }
        return modelMap;
    }

    @RequestMapping(value = "/queryrelative")
    @ResponseBody
    public Map<String,Object> queryRelative(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        Integer relativeId = HttpServletRequestUtil.getInt(request,"relative_id");
        Integer disablerId = HttpServletRequestUtil.getInt(request,"disabler_id");
        if (relativeId!=-1 && disablerId!=-1){
            RelativeLocationExecution rl = relativeLocationService.QueryRelationLocation(relativeId);
            QueryLocationExecution ql = queryLocationService.AddQueryLocation(disablerId,relativeId,1);
            if (rl.getState()==LocationStateEnum.SUCCESS_QUERY.getState()&&ql.getState()==LocationStateEnum.SUCCESS_ADDQUERY.getState()){
                modelMap.put("success",true);
                modelMap.put("queryRecord",ql.getQueryLocation());
                modelMap.put("relativeLocation",rl.getRelativeLocation());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",rl.getStateInfo());
            }
        }else {
            modelMap.put("success",false);
            modelMap.put("errMsg","请输入完整信息");
        }
        return modelMap;
    }

    @RequestMapping(value = "/querydisablerrecord")
    @ResponseBody
    public Map<String,Object> queryDisablerRecord(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        Integer disablerId = HttpServletRequestUtil.getInt(request,"disabler_id");
        Integer status = HttpServletRequestUtil.getInt(request,"status");
        if (disablerId!=-1 && status !=-1){
            QueryLocationExecution ql = queryLocationService.QueryLocationByDisablerId(disablerId,status);
            if (ql.getState()==LocationStateEnum.SUCCESS_QUERY_QUERY.getState()){
                modelMap.put("success",true);
                modelMap.put("queryDisablerRecord",ql.getQueryLocationList());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",ql.getStateInfo());
            }
        }else {
            modelMap.put("success",false);
            modelMap.put("errMsg","请输入完整信息");
        }
        return modelMap;
    }

    @RequestMapping(value = "/queryrelativerecord")
    @ResponseBody
    public Map<String,Object> queryRelativeRecord(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        Integer relativeId = HttpServletRequestUtil.getInt(request,"relative_id");
        Integer status = HttpServletRequestUtil.getInt(request,"status");
        if (relativeId!=-1 && status !=-1){
            QueryLocationExecution ql = queryLocationService.QueryLocationByRelativeId(relativeId,status);
            if (ql.getState()==LocationStateEnum.SUCCESS_QUERY_QUERY.getState()){
                modelMap.put("success",true);
                modelMap.put("queryRelativeRecord",ql.getQueryLocationList());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",ql.getStateInfo());
            }
        }else {
            modelMap.put("success",false);
            modelMap.put("errMsg","请输入完整信息");
        }
        return modelMap;
    }
}
